package com.insist.controller;

import com.insist.entity.ChargeInfo;
import com.insist.framework.CmmBaseResponse;
import com.insist.model.ChargeModel;
import com.insist.service.ChargeService;
import com.insist.util.BeanCopierUtils;
import com.insist.util.CmmUtils;
import com.insist.util.MD5Util;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by INSIST on 2016/7/2.
 */
@Controller
@RequestMapping(value = "/charge")
public class ChargeController {

    Logger logger = Logger.getLogger(ChargeController.class);

    @Autowired
    private ChargeService chargeService;

    @RequestMapping("/createChargeOrder")
    @ResponseBody
    public CmmBaseResponse createChargeOrder(Double amount){
        if(amount == null||amount<=0){
            CmmBaseResponse response = CmmBaseResponse.buildResponse(false,"充值额度错误");
        }
        ChargeInfo info = chargeService.createChargeOrder(CmmUtils.getLoginUserId(), amount);
        CmmBaseResponse response = CmmBaseResponse.buildResponse(true,"创建成功");
        if(CmmUtils.IS_DEV){
            info.setAmount(0.01);
        }
        ChargeModel model = new ChargeModel();
        BeanCopierUtils.copyProperties(info, model);
        model.setTitle("车咪咪充值订单");
        model.setReturnUrl(CmmUtils.chargeReturnUrl);
        String temp = CmmUtils.keyId + "车咪咪充值订单" + Double.valueOf(info.getAmount() * 100).intValue() + info.getPayNo() + CmmUtils.keySecret;
        model.setSign(MD5Util.MD5(temp));
        response.setData(model);
        return response;
    }

    @RequestMapping("/findChargeOrder")
    @ResponseBody
    public List<ChargeInfo> findChargeOrder(){
        return chargeService.findChargeOrder(CmmUtils.getLoginUserId());
    }

}
